package com.yjhb.android.sbz.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.Conf;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.mvp.feedback.FeedbackPresenter;
import com.yjhb.android.sbz.mvp.feedback.FeedbackView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.tool.ZxTool;
import com.jojo.android.zxlib.view.nine.ItemImageClickListener;
import com.jojo.android.zxlib.view.nine.NineGridImageView;
import com.jojo.android.zxlib.view.nine.NineGridImageViewAdapter;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/4 11:37
 */
public class FeedbackActivity extends BaseActivity<FeedbackPresenter> implements ItemImageClickListener, FeedbackView {
    private final String TAG = "FeedbackActivity";
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.upload_iv)
    ImageView uploadIv;
    @BindView(R.id.nineGridImageView)
    NineGridImageView nineGridImageView;
    private NineGridImageViewAdapter<LocalMedia> adapter;
    private List<LocalMedia> imgPaths = new ArrayList<>();

    @Override
    protected FeedbackPresenter initPresenter() {
        return new FeedbackPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initUI() {
        nineGridImageView.setAdapter(adapter = new NineGridImageViewAdapter<LocalMedia>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, LocalMedia o) {
                Glide.with(context).load(o.getPath())
                        .apply(new RequestOptions().placeholder(com.jojo.android.zxlib.R.drawable.xui_ic_default_img))
                        .into(imageView);
            }
        });
        nineGridImageView.setMaxSize(9);
        nineGridImageView.setSingleImgSize(ZxTool.dip2px(30));
        nineGridImageView.setShowStyle(0);
    }

    @Override
    protected void initData() {

    }

    private boolean check() {
        if (Tool.isEditEmpty(etTitle)) {
            ZxToast.showINFOBottomToast("标题不能为空");
            return false;
        }
        if (Tool.isEditEmpty(etContent)) {
            ZxToast.showINFOBottomToast("反馈内容不能为空");
            return false;
        }
        if (imgPaths.size() < 1) {
            ZxToast.showINFOBottomToast("请上传相关截图");
            return false;
        }
        if (Tool.getText(etTitle).length() > 20) {
            ZxToast.showINFOBottomToast("标题最多只能填写20字");
            return false;
        }
        if (Tool.getText(etContent).length() > 500) {
            ZxToast.showINFOBottomToast("反馈内容最多只能填写500字");
            return false;
        }
        return true;
    }

    @Override
    protected void regEvent() {
        nineGridImageView.setItemImageClickListener(this);
    }

    @Override
    public void refresh() {
        nineGridImageView.setImagesData(new ArrayList<>(imgPaths));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Conf.REQUEST_IMAGE && resultCode == RESULT_OK) {
            imgPaths.clear();
            imgPaths.addAll((ArrayList<LocalMedia>) data.getSerializableExtra(PictureConfig.EXTRA_RESULT_SELECTION));
            refresh();
        }
    }

    @OnClick(R.id.upload_iv)
    void onUpload() {
        Tool.getPictureSelector(this, 9)
                .selectionMedia(imgPaths)
                .forResult(Conf.REQUEST_IMAGE);
    }

    @OnClick(R.id.back)
    void onBack(){
        finish();
    }


    @Override
    public void onItemImageClick(ImageView imageView, int index, List list) {
        onUpload();
    }

    @OnClick(R.id.content_layout)
    void content_layout() {
        etContent.setFocusable(true);
        etContent.requestFocus();
        ZxTool.openKeybord(etContent, this);
    }

    @OnClick(R.id.title_layout)
    void title_layout() {
        etTitle.setFocusable(true);
        etTitle.requestFocus();
        ZxTool.openKeybord(etTitle, this);
    }

    @OnClick(R.id.submit_tv)
    void submit_tv() {
        if (check()) {
            presenter.feedback(Tool.getText(etTitle), Tool.getText(etContent), imgPaths);
        }
    }


    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.show();
        ZxToast.showFAILBottomToast(msg);
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onSuccess() {
        loadingDialog.show();
        ZxToast.showSuccessBottomToast("反馈已提交。");
        finish();
    }
}
